package com.changgou.service.goods.service.impl;

import com.changgou.goods.pojo.Brand;
import com.changgou.service.goods.dao.BrandMapper;
import com.changgou.service.goods.service.BrandService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
public class BrandServiceImpl implements BrandService {
    @Autowired
    private BrandMapper brandMapper;
    @Override
    public List<Brand> findAll() {
        List<Brand> brandList = brandMapper.selectAll();
        return brandList;
    }

    @Override
    public Brand findById(int id) {
        return brandMapper.selectByPrimaryKey(id);
    }

    @Override
    public void add(Brand brand) {
        brandMapper.insertSelective(brand);
    }

    @Override
    public void delete(Integer id) {
        brandMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(Brand brand) {
        brandMapper.updateByPrimaryKeySelective(brand);
    }

    @Override
    public List<Brand> findList(Map<String, Object> map) {
        Example example = new Example(Brand.class);
        Example.Criteria criteria = example.createCriteria();
        if(map != null){
            //品牌名称模糊查询
            if((map.get("name")!=null && !"".equals(map.get("name")))){
                criteria.andLike("name","%"+map.get("name")+"%");
            }
            //品牌首字母
            if((map.get("letter")!=null && !"".equals(map.get("letter")))){
                criteria.andEqualTo("letter",map.get("letter"));
            }
        }
        return brandMapper.selectByExample(example);
    }

    @Override
    public Page<Brand> findPage(int currentPage, int size) {
        PageHelper.startPage(currentPage-1,size);
        Page<Brand> page = (Page<Brand>) brandMapper.selectAll();
        return page;
    }

    @Override
    public Page<Brand> findListAndfindPage(Map<String, Object> map, int currentPage, int size) {
        PageHelper.startPage(currentPage-1,size);
        Example example = new Example(Brand.class);
        Example.Criteria criteria = example.createCriteria();
        if(map != null){
            //品牌名称模糊查询
            if((map.get("name")!=null && !"".equals(map.get("name")))){
                criteria.andLike("name","%"+map.get("name")+"%");
            }
            //品牌首字母
            if((map.get("letter")!=null && !"".equals(map.get("letter")))){
                criteria.andEqualTo("letter",map.get("letter"));
            }
        }
        return (Page<Brand>)brandMapper.selectByExample(example);
    }
}
